package cn.t.facade.order;


import cn.t.constants.ResponseError;
import cn.t.converter.order.OrderDetailDTOConverter;
import cn.t.converter.order.OrderModelConverter;
import cn.t.core.exception.BizException;
import cn.t.core.facade.FacadeImpl;
import cn.t.dto.device.CargowayDTO;
import cn.t.dto.order.OrderDetailDTO;
import cn.t.dto.sku.SkuDTO;
import cn.t.dto.user.RoleSkuDTO;
import cn.t.facade.device.CargowayFacade;
import cn.t.facade.sku.SkuFacade;
import cn.t.facade.user.RoleSkuFacade;
import cn.t.model.order.OrderDO;
import cn.t.model.order.OrderDetailDO;
import cn.t.service.order.OrderDetailService;
import cn.t.vo.order.DislodgeOrderDetailVO;
import cn.t.vo.order.UpdateOrderGoodsVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 领料订单明细表(OrderDetail)服务编排
 *
 * @author t
 * @since 2024-05-24 10:19:34
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderDetailFacade extends FacadeImpl<OrderDetailDTO, OrderDetailDO, OrderDetailDTOConverter, OrderDetailService> {
    @Resource
    private SkuFacade skuFacade;
    @Resource
    private CargowayFacade cargowayFacade;
    @Resource
    private RoleSkuFacade roleSkuFacade;
    @Resource
    private WorkOrderFacade workOrderFacade;
    @Transactional
    public Boolean saveOrUpdate(List<UpdateOrderGoodsVO> orderGoodsList, OrderDO orderDO) {

        Long deviceId = orderDO.getDeviceId();
        // 获取sku信息
        List<String> skus = orderGoodsList.stream().map(orderGoods -> orderGoods.getSkuCode().trim()).collect(Collectors.toList());
        checkRoleDeviceSku(orderDO, skus);
        //获取商品信息
        List<SkuDTO> skuTtoList = skuFacade.findModelList(skus);
        if (CollectionUtils.isEmpty(skuTtoList)) {
            throw new BizException(ResponseError.OPTION_DATA_NOT_FOUND, String.format(ResponseError.OPTION_DATA_NOT_FOUND.getName(), "sku"));
        }
        Map<String, SkuDTO> goodsDoMap = skuTtoList.stream().
                collect(Collectors.toMap(SkuDTO::getSkuCode, Function.identity()));
        //查询货道信息
        List<CargowayDTO> modelList = cargowayFacade.getInfoByDeviceId(deviceId);
        Map<Integer, CargowayDTO> carGowayDoMap = modelList.stream().collect(Collectors.toMap(
                CargowayDTO::getCargowayNo, Function.identity()));

        List<OrderDetailDTO> orderDetailDto = new ArrayList<>();

        for (UpdateOrderGoodsVO orderGoodVo : orderGoodsList) {
            //获取商品信息
            SkuDTO goodsDto = goodsDoMap.get(orderGoodVo.getSkuCode());
            CargowayDTO cargowayDto = carGowayDoMap.get(orderGoodVo.getCargowayNo());
            if (ObjectUtils.isEmpty(goodsDto)) {
                throw new BizException(ResponseError.SKU_NOT_EXIST, String.format(ResponseError.SKU_NOT_EXIST.getName(), orderGoodVo.getSkuCode()));
            }
            if (ObjectUtils.isEmpty(cargowayDto)) {
                throw new BizException(ResponseError.OPTION_DATA_NOT_FOUND, String.format(ResponseError.OPTION_DATA_NOT_FOUND.getName(), "货道"));
            }
            OrderDetailDTO orderGoodsDto = OrderModelConverter.convertVoToOrderDetail(goodsDto, orderGoodVo, orderDO);
            orderGoodsDto.setCargowayId(cargowayDto.getId());
            orderGoodsDto.setOrderSn(orderDO.getOrderSn());
            orderDetailDto.add(orderGoodsDto);
        }
        if (CollectionUtils.isEmpty(orderDetailDto)) {
            throw new BizException(ResponseError.OPTION_DATA_NOT_FOUND, String.format(ResponseError.OPTION_DATA_NOT_FOUND.getName(), "订单明细！"));
        }

        return    service.saveOrUpdateBatch( converter.dto2DoList(orderDetailDto));

    }

    private  void checkRoleDeviceSku(OrderDO orderDO,List<String> skus) {
        //校验设备与用户的绑定
        List<RoleSkuDTO> results = roleSkuFacade.findSkuByCardCode(orderDO.getFirmId(), orderDO.getCardCode().trim(), orderDO.getDeviceId());
        //商品权限校验
        if (CollectionUtils.isEmpty(results)) {
            throw new BizException(ResponseError.USER_ROLE_ERR, ResponseError.USER_ROLE_ERR.getName());
        }
        List<String> roleSku = results.stream().map(RoleSkuDTO::getSkuCode).distinct().collect(Collectors.toList());
        if ( skus.stream().anyMatch(skuCode -> !roleSku.contains(skuCode))) {
            throw new BizException(ResponseError.USER_ROLE_ERR, ResponseError.USER_ROLE_ERR.getName());
        }
    }

    public Boolean deleteOrderDetail(String orderSn,List<Long> ids) {
        OrderDetailDO template = new OrderDetailDO();
        template.setOrderSn(orderSn);
      return   service.deleteOrderDetail(template,ids);

    }
    public List<DislodgeOrderDetailVO> getDislodgeOrderDetail(OrderDetailDO orderDetailDO) {
        List<OrderDetailDO> orderDetails = service.queryListByOrderSn(orderDetailDO);
        if (CollectionUtils.isEmpty(orderDetails)){
            return Collections.emptyList();
        }
       return orderDetails.stream().map(OrderModelConverter::convertDislodgeDetail).collect(Collectors.toList());
    }

}

